package net.xinqushi.design.templateMethod.core;

import java.util.Properties;

import net.xinqushi.design.templateMethod.util.NumberUtils;

/**
 *   静态工厂方法，参考Factory包
 */
public class MessageSenderFactory {

	private static final String PROP_HOST="Host";
	private static final String PROP_PORT="Port";
	private static final String PROP_USERNAME="Username";
	private static final String PROP_PASSWORD="Password";
	private static final String PROP_READBUFFERSIZE="ReadBufferSize";
	private static final String PROP_WRITEBUFFERSIZE="WriteBufferSize";
	private static final String PROP_CONNECTWAITTIME="ConnectWaitTime";
	private static final String PROP_MAXACTIVECONNECTION="MaxActiveConnection";
	private static final String PROP_POOLBLOCKINGTIME="PoolBlockingTime";
	private static final String PROP_READBLOCKINGTIME="ReadTimeout";
	private static final String PROP_KEEPALIVEIDLETIME="KeepAliveIdleTime";
	private static final String PROP_SCHEDULERENABLED="SchedulerEnabled";

	public static SimpleSocketMessageSender newSimpleSocketMessageSender(Properties properties)throws Exception {
		String value = null;
		String host=null;
		int port;
		String username=null;
		String password=null;

		value=properties.getProperty(PROP_HOST);
		if (value != null) {
			host=value;
		}else{
			throw new Exception("Host is null!");
		}

		value=properties.getProperty(PROP_PORT);
		if (value != null) {
			port=NumberUtils.parseNumber(value, Integer.class);
		}else{
			throw new Exception("Port is null!");
		}

		value=properties.getProperty(PROP_USERNAME);
		if (value != null) {
			username=value;
		}else{
			throw new Exception("Username is null!");
		}

		value=properties.getProperty(PROP_PASSWORD);
		if (value != null) {
			password=value;
		}else{
			throw new Exception("Password is null!");
		}

		SimpleSocketMessageSender sender=new SimpleSocketMessageSender(host,port,username,password);

		value=properties.getProperty(PROP_READBUFFERSIZE);
		if (value != null) {
			sender.setReadBufferSize(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_WRITEBUFFERSIZE);
		if (value != null) {
			sender.setWriteBufferSize(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_CONNECTWAITTIME);
		if (value != null) {
			sender.setWaitTimeout(NumberUtils.parseNumber(value, Integer.class));
		}
		
		value=properties.getProperty(PROP_READBLOCKINGTIME);
		if (value != null) {
			sender.setReadTimeout(NumberUtils.parseNumber(value, Integer.class));
		}
		
		return sender;
	}

	public static PoolableSocketMessageSender newPoolableSocketMessageSender(Properties properties)throws Exception {
		String value = null;
		String host=null;
		int port;
		String username=null;
		String password=null;

		value=properties.getProperty(PROP_HOST);
		if (value != null) {
			host=value;
		}else{
			throw new Exception("Host is null!");
		}

		value=properties.getProperty(PROP_PORT);
		if (value != null) {
			port=NumberUtils.parseNumber(value, Integer.class);
		}else{
			throw new Exception("Port is null!");
		}

		value=properties.getProperty(PROP_USERNAME);
		if (value != null) {
			username=value;
		}else{
			throw new Exception("Username is null!");
		}

		value=properties.getProperty(PROP_PASSWORD);
		if (value != null) {
			password=value;
		}else{
			throw new Exception("Password is null!");
		}

		PoolableSocketMessageSender sender=new PoolableSocketMessageSender(host,port,username,password);

		value=properties.getProperty(PROP_READBUFFERSIZE);
		if (value != null) {
			sender.setReadBufferSize(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_WRITEBUFFERSIZE);
		if (value != null) {
			sender.setWriteBufferSize(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_CONNECTWAITTIME);
		if (value != null) {
			sender.setWaitTimeout(NumberUtils.parseNumber(value, Integer.class));
		}
		
		value=properties.getProperty(PROP_READBLOCKINGTIME);
		if (value != null) {
			sender.setReadTimeout(NumberUtils.parseNumber(value, Integer.class));
		}
		
		value=properties.getProperty(PROP_MAXACTIVECONNECTION);
		if (value != null) {
			sender.setMaxActive(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_POOLBLOCKINGTIME);
		if (value != null) {
			sender.setMaxWait(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_KEEPALIVEIDLETIME);
		if (value != null) {
			sender.setKeepIdleTime(NumberUtils.parseNumber(value, Integer.class));
		}

		return sender;
	}

	public static SchedulerPoolableSocketMessageSender newSchedulerPoolableSocketMessageSender(Properties properties)throws Exception {
		String value = null;
		String host=null;
		int port;
		String username=null;
		String password=null;

		value=properties.getProperty(PROP_HOST);
		if (value != null) {
			host=value;
		}else{
			throw new Exception("Host is null!");
		}

		value=properties.getProperty(PROP_PORT);
		if (value != null) {
			port=NumberUtils.parseNumber(value, Integer.class);
		}else{
			throw new Exception("Port is null!");
		}

		value=properties.getProperty(PROP_USERNAME);
		if (value != null) {
			username=value;
		}else{
			throw new Exception("Username is null!");
		}

		value=properties.getProperty(PROP_PASSWORD);
		if (value != null) {
			password=value;
		}else{
			throw new Exception("Password is null!");
		}

		SchedulerPoolableSocketMessageSender sender=new SchedulerPoolableSocketMessageSender(host,port,username,password);

		value=properties.getProperty(PROP_READBUFFERSIZE);
		if (value != null) {
			sender.setReadBufferSize(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_WRITEBUFFERSIZE);
		if (value != null) {
			sender.setWriteBufferSize(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_CONNECTWAITTIME);
		if (value != null) {
			sender.setWaitTimeout(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_READBLOCKINGTIME);
		if (value != null) {
			sender.setReadTimeout(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_MAXACTIVECONNECTION);
		if (value != null) {
			sender.setMaxActive(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_POOLBLOCKINGTIME);
		if (value != null) {
			sender.setMaxWait(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_KEEPALIVEIDLETIME);
		if (value != null) {
			sender.setKeepIdleTime(NumberUtils.parseNumber(value, Integer.class));
		}

		value=properties.getProperty(PROP_SCHEDULERENABLED);
		if (value != null) {
			sender.setEnabled(Boolean.parseBoolean(value));
		}

		return sender;
	}
}
